package com.zkingsoft.actions.feedback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.model.Bug;
import com.zkingsoft.model.Changes;
import com.zkingsoft.model.CodeReview;
import com.zkingsoft.model.InterfaceCate;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Task;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.MoneyUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BugService;
import com.zkingsoft.services.ChangesService;
import com.zkingsoft.services.CodeReviewService;
import com.zkingsoft.services.InterfaceCateService;
import com.zkingsoft.services.ProjPostService;
import com.zkingsoft.services.ProjUserService;
import com.zkingsoft.services.ProjectService;
import com.zkingsoft.services.TaskService;
import com.zkingsoft.services.UsersService;
import com.zkingsoft.thread.SendFeedbackOaMsgsThread;

/**
 * @author dc
 * @date 2016-11-15 11:54
 */
@Controller
@RequestMapping(value = "feedback/clientchanges")
public class ClientChangesController extends BaseController {

	@Resource
	private ChangesService changesService;
	@Resource
	private UsersService usersService;
	@Resource
	private ProjectService projectService;
	@Resource
	private ProjUserService projUserService;
	@Resource
	private TaskService taskService;
	@Resource
	private BugService bugService;
	@Resource
	private CodeReviewService  codeReviewService;
	@Resource
	private InterfaceCateService interfaceCateService;
	@Resource
	private ProjPostService projPostService;

	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(Changes changes, PaginationVO pageVo) {
		changes.setCreateUserId(getClientMe().getId());
		pageVo.setOrder("desc");
		pageVo.setSort("createTime");
		return showList(changesService, changes, pageVo);
	}

	/**
	 * 对客户的问题反馈进行处理
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Changes changes) {
		if (changes.getId() != null) {
			int modifyResult = changesService.modify(changes);
			if (modifyResult > 0) {
				//发送oa消息
				SendFeedbackOaMsgsThread sfomt = 
						new SendFeedbackOaMsgsThread(projectService, changesService, 
								changes, Dictionary.BG_COLOR_FENDBACK, Dictionary.TITLE_FENDBACK_MODIFY);
				new Thread(sfomt).start();
				return new AjaxResult(AjaxResult.STATUS_OK, null, "问题反馈修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, "问题反馈修改失败");
			}
		} else {
			changes.setCreateTime(new Date());
			changes.setCreateUserId(getClientMe().getId());
			changes.setHandleResult(Dictionary.TASK_RESULT_STATUS_WAIT);
			int addResult = changesService.add(changes);
			if (addResult > 0) {
				//发送oa消息
				SendFeedbackOaMsgsThread sfomt = 
						new SendFeedbackOaMsgsThread(projectService, changesService, 
								changes,Dictionary.BG_COLOR_FENDBACK, Dictionary.TITLE_FENDBACK_NEW);
				new Thread(sfomt).start();
				return new AjaxResult(AjaxResult.STATUS_OK, null, "问题反馈添加成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_ERR, null, "问题反馈添加失败");
			}
		}	
	}

	/**
	 * 进入对客户的问题反馈进行处理的页面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Changes changes) {
		
		Long projId = changes.getProjId();
		Long id = changes.getId();
		if (id != null) {
			Changes	changesdata= changesService.findById(id);
			projId = changesdata.getProjId();
			if(changes.getOptoinType()!=null){
				changesdata.setId(null);
				changesdata.setTitle("【重新处理】"+changesdata.getTitle());
			}
			WebUtil.getRequest().setAttribute("obj", changesdata);
		}
		WebUtil.getRequest().setAttribute("projId", projId);
		return "feedback/feedback-form";
	}

	/**
	 * 进入查看客户反馈问题列表
	 */
	@RequestMapping(value = "/showFeedBackList")
	public String showFeedBackList(Long projId) {
		/*WebUtil.getRequest().setAttribute("projId", projId);*/
		return "feedback/feedback_list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(changesService, keys);
	}
	
	
	@RequestMapping(value = "/showFeedback")
	public String showFeedback(Long id) {		
		Changes changes;
		if (id != null) {
			changes = changesService.findById(id);
			WebUtil.getRequest().setAttribute("obj", changes);
		}
		return "feedback/feedback-show";
		
	}
	
	/**
	 * 查询项目中所有的分类
	 */
	@RequestMapping(value = "/showCateList")
	public @ResponseBody AjaxResult showCateList(InterfaceCate interfaceCate) {
		if (interfaceCate.getProjId() != null) {
			return new AjaxResult(AjaxResult.STATUS_OK, null, null, interfaceCateService.findByModel(interfaceCate), 0);
		}
		return new AjaxResult(AjaxResult.STATUS_OK, null,"项目id为null");
	}
	
	
	/**
	 * @Description:查询客户拥有的项目
	 * @author:dingchuan
	 * @param project
	 * @param pageVo
	 * @return 返回类型 AjaxResult
	 * @date 2016年11月16日
	 */
	@RequestMapping(value = "/queryClientProj")
	public @ResponseBody AjaxResult queryClientProj(Project project, PaginationVO pageVo) {
		project.setCustId(getClientMe().getId());
		return showList(projectService, project, pageVo);
	}
	
	/**
	 * 查询项目详细信息
	 */
	@RequestMapping(value = "/all")
	public String all(Long id) {
		Project project = projectService.findById(id);
		// 项目详情
		WebUtil.getRequest().setAttribute("obj", project);
		long totalTime = project.getEndTime().getTime() - project.getCreateTime().getTime();
		long remainingTime = new Date().getTime() - project.getCreateTime().getTime();
		double proportion = MoneyUtil.mul(MoneyUtil.div(remainingTime, totalTime, 2), 100);
		WebUtil.getRequest().setAttribute("proportion", proportion);

		// 开发人员
		ProjUser projectUser = new ProjUser();
		projectUser.setProjId(id);
		WebUtil.getRequest().setAttribute("projectUserList", projUserService.findByModel(projectUser));
		return "feedback/projectInfo-list";
	}

	// 任务查询
	@RequestMapping(value = "/queryTask")
	public @ResponseBody AjaxResult projStart(Task task) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, taskService.findByModel(task), 0);
	}

	// bug查询
	@RequestMapping(value = "/queryBug")
	public @ResponseBody AjaxResult queryBug(Bug bug) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, bugService.findByModel(bug), 0);
	}

	// 加载所有的开发人员
	@RequestMapping(value = "/queryPostName")
	public @ResponseBody AjaxResult queryPostName(long projId) {

		ProjUser projUser = new ProjUser();
		projUser.setProjId(projId);
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, projUserService.findByModel(projUser), 0);

	}

	// 评审得分查询
	@RequestMapping(value = "/queryCodeReview")
	public @ResponseBody AjaxResult queryCodeReview(CodeReview codeReview) {
		return new AjaxResult(AjaxResult.STATUS_OK, null, "修改成功", codeReviewService.findByModel(codeReview), 0);
	}

}